<?php

    function    sxml2array ( $line ) 
    {
        return sxml2array_strpos($line);
    }


    function    sxml2array_char ( $line )
    {
        $lineinfo = array ();
        $len = strlen($line);
        $stage = 0;
        $tag_name_start = 0;
        $tag_name_stop = 0;
        $tag_data_start = 0;
        $tag_data_stop = 0;
        $tag_name = '';
        $tag_data = '';
        for ($i=0; $i<$len; $i++) {
            switch ( $stage ) {
            case    0   :
                if ( '<' == $line[$i] ) {
                    $tag_name_start = $i + 1;
                    $stage = 1;
                }
                break;
            case    1   :
                if ( ':' == $line[$i] ) {
                    $tag_name_stop = $i - 1;
                    $tag_name = trim( substr ( $line, $tag_name_start, $i - $tag_name_start ) );
                    //echo "mymatch2 : $tag_name\n";
                    $tag_data_start = $i + 1;
                    $stage = 2;
                }
                break;
            case    2   :
                if ( '>' == $line[$i] ) {
                    $tag_data_stop = $i - 1;
                    $tag_data = trim( substr ( $line, $tag_data_start, $i - $tag_data_start ) );
                    $lineinfo [ $tag_name ] = $tag_data;
                    $stage = 0;
                }
                break;
            }
        }

        return  $lineinfo;
    }   // sxml2array()


    function    sxml2array_strpos ( $line )
    {
        $lineinfo = array ();
        $len = strlen($line);
        $stage = 0;
        $tag_name_start = 0;
        $tag_name_stop = 0;
        $tag_data_start = 0;
        $tag_data_stop = 0;
        $tag_name = '';
        $tag_data = '';
        
        while (true) {
            $tag_name_start = strpos($line, '<', $tag_data_stop);
            if (false === $tag_name_start) {
                break;
            }
            $tag_name_start += 1;

            $tag_name_stop = strpos($line, ':', $tag_name_start);
            if (false === $tag_name_stop) {
                break;
            }
            $tag_data_start = $tag_name_stop + 1;

            $tag_data_stop = strpos($line, '>', $tag_data_start);
            if (false === $tag_data_stop) {
                break;
            }

            $tag_name = substr($line, $tag_name_start, $tag_name_stop - $tag_name_start);
            $tag_data = substr($line, $tag_data_start, $tag_data_stop - $tag_data_start);

            $lineinfo[ $tag_name ] = $tag_data;
        }

        return  $lineinfo;
    }   // sxml2array()

